package org.example.Polls;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.List;

import com.thoughtworks.xstream.XStream;

public class SOAPUserService implements UserService {
	private URL serviceUrl;
	
	public SOAPUserService(String url) throws MalformedURLException {
		serviceUrl = new URL(url);
	}

	public User createUser(User user) throws Exception {
		XStream xstream = XStreamManager.getInstance().getXStream();
		System.out.println(xstream.toXML(user));
		Object[] values = {user.getEmail(), user.getPassword(), user.getFirstName(), user.getLastName(), user.getAlias()};
		SOAPHelper soapHelper = SOAPHelper.getInstance();
		String response = soapHelper.sendRequest(serviceUrl, "create_user", values);
		SOAPUnmarshaller unmarshaller = new SOAPUnmarshaller(response);
		List userList = unmarshaller.unmarshal("//user");
		if (userList == null || userList.size() == 0 || !(userList.get(0) instanceof User))
			throw new Exception("Unknown response received!");
		return (User)userList.get(0);
	}
	
}
